import { formatGMTDate, getYMD, getNowFormatDate, getDay, compareObject } from '../../../../assets/js/util.js'
export default {
    data() {
        return {
            // 默认参数 和 URL
            SRC: '',
            DP: {
                p: '',
                f: '',
                uid: ''
            },
            /*=========时间段配置=========*/
            pickerOptions: {
                shortcuts: [{
                    text: '近7天',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '近15天',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 15);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '近30天',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                        picker.$emit('pick', [start, end]);
                    }
                }]
            },
            title: '广告日数据（留存/付费）',
            agents: [],
            attrs: [],
            channels: [],
            agents: [],
            platForms: [],
            settlWays: [],
            adPlaces: [],
            ruleForm: {
                date: '',
                attr: '',
                channel: '',
                agent: '',
                platForm: '',
                settlWay: '',
                adPlace: '',
            },
            /*=================表1数据=================*/
            tableHead: [],
            tableHead1: [{
                    "prop": "dateStr",
                    "label": "日期"
                },
                {
                    "prop": "newAccount",
                    "label": "新增账号"
                },
                {
                    "prop": "remain2",
                    "label": "次日留存"
                },
                {
                    "prop": "remain3",
                    "label": "3日留存"
                },
                {
                    "prop": "remain4",
                    "label": "4日留存"
                },
                {
                    "prop": "remain5",
                    "label": "5日留存"
                },
                {
                    "prop": "remain6",
                    "label": "6日留存"
                },
                {
                    "prop": "remain7",
                    "label": "7日留存"
                },
                {
                    "prop": "remain8",
                    "label": "8日留存"
                },
                {
                    "prop": "remain9",
                    "label": "9日留存"
                },
                {
                    "prop": "remain10",
                    "label": "10日留存"
                },
                {
                    "prop": "remain11",
                    "label": "11日留存"
                },
                {
                    "prop": "remain12",
                    "label": "12日留存"
                },
                {
                    "prop": "remain13",
                    "label": "13日留存"
                },
                {
                    "prop": "remain14",
                    "label": "14日留存"
                },
                {
                    "prop": "remain15",
                    "label": "15日留存"
                },
                {
                    "prop": "remain16",
                    "label": "16日留存"
                },
                {
                    "prop": "remain17",
                    "label": "17日留存"
                },
                {
                    "prop": "remain18",
                    "label": "18日留存"
                },
                {
                    "prop": "remain19",
                    "label": "19日留存"
                },
                {
                    "prop": "remain20",
                    "label": "20日留存"
                },
                {
                    "prop": "remain21",
                    "label": "21日留存"
                },
                {
                    "prop": "remain22",
                    "label": "22日留存"
                },
                {
                    "prop": "remain23",
                    "label": "23日留存"
                },
                {
                    "prop": "remain24",
                    "label": "24日留存"
                },
                {
                    "prop": "remain25",
                    "label": "25日留存"
                },
                {
                    "prop": "remain26",
                    "label": "26日留存"
                },
                {
                    "prop": "remain27",
                    "label": "27日留存"
                },
                {
                    "prop": "remain28",
                    "label": "28日留存"
                },
                {
                    "prop": "remain29",
                    "label": "29日留存"
                },
                {
                    "prop": "remain30",
                    "label": "30日留存"
                },
                {
                    "prop": "remain60",
                    "label": "60日留存"
                },
                {
                    "prop": "remain90",
                    "label": "90日留存"
                }
            ],
            tableHead2: [{
                    "prop": "0",
                    "label": "日期"
                },
                {
                    "prop": "1",
                    "label": "新增账号"
                },
                {
                    "prop": "2",
                    "label": "1日收入"
                },
                {
                    "prop": "3",
                    "label": "2日收入"
                },
                {
                    "prop": "4",
                    "label": "3日收入"
                },
                {
                    "prop": "5",
                    "label": "4日收入"
                },
                {
                    "prop": "6",
                    "label": "5日收入"
                },
                {
                    "prop": "7",
                    "label": "6日收入"
                },
                {
                    "prop": "8",
                    "label": "7日收入"
                },
                {
                    "prop": "9",
                    "label": "8日收入"
                },
                {
                    "prop": "10",
                    "label": "9日收入"
                },
                {
                    "prop": "11",
                    "label": "10日收入"
                },
                {
                    "prop": "12",
                    "label": "11日收入"
                },
                {
                    "prop": "13",
                    "label": "12日收入"
                },
                {
                    "prop": "14",
                    "label": "13日收入"
                },
                {
                    "prop": "15",
                    "label": "14日收入"
                },
                {
                    "prop": "16",
                    "label": "15日收入"
                },
                {
                    "prop": "17",
                    "label": "16日收入"
                },
                {
                    "prop": "18",
                    "label": "17日收入"
                },
                {
                    "prop": "19",
                    "label": "18日收入"
                },
                {
                    "prop": "20",
                    "label": "19日收入"
                },
                {
                    "prop": "21",
                    "label": "20日收入"
                },
                {
                    "prop": "22",
                    "label": "21日收入"
                },
                {
                    "prop": "23",
                    "label": "22日收入"
                },
                {
                    "prop": "24",
                    "label": "23日收入"
                },
                {
                    "prop": "25",
                    "label": "24日收入"
                },
                {
                    "prop": "26",
                    "label": "25日收入"
                },
                {
                    "prop": "27",
                    "label": "26日收入"
                },
                {
                    "prop": "28",
                    "label": "27日收入"
                },
                {
                    "prop": "29",
                    "label": "28日收入"
                },
                {
                    "prop": "30",
                    "label": "29日收入"
                },
                {
                    "prop": "31",
                    "label": "30日收入"
                },
                {
                    "prop": "61",
                    "label": "60日收入"
                },
                {
                    "prop": "91",
                    "label": "90日收入"
                }
            ],
            activeIndex: '0',
            loading: false,
            searchable: false,
            tableData: [],
            tableDataAll: [],
            /*当前页显示数量*/
            pageSize: 10,
            /*每页显示的数量*/
            pageSizes: [10, 20, 50, 100],
            /*dataTotal表数量*/
            dataTotal: 0,
            /*分页器*/
            currentPage: 1,
        }
    },
    computed: {

    },
    methods: {
        // 通用 成功 与 失败
        successFun(data) {
            this.$message({
                message: data,
                type: 'success'
            });
        },
        errorFun(data) {
            this.$message.error(data);
        },
        dateChange(val) {
            // this.ruleForm.date = val;
        },
        attrChange() {

        },
        channelChange() {

        },
        agentChange() {

        },
        platFormChange() {

        },
        settlWayChange() {

        },
        adPlaceChange() {

        },
        handleSizeChange(val) {
            this.pageSize = val;
            this.tableControl();

        },
        handleCurrentChange(val) {
            this.currentPage = val;
            this.tableControl();
        },

        tableControl() {
            this.tableData = this.tableDataAll.slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize)
        },
        loads() {
            this.loading = !this.loading; //加载中
            this.searchable = !this.searchable; //查询按钮不可用
        },
        /*==================获取下拉列表数据========================*/
        getData() {
            this.$http.jsonp(`${this.SRC}data/get.do?p=${this.DP.p}&f=${this.DP.f}&a=initParam`, {
                params: { 'ps': `uid=${this.DP.uid}` }
            }).then(function(res) {
                var data = JSON.parse(JSON.parse(res.body));
                if (parseInt(data['res']) === 0) {
                    /*=====获取归属======*/
                    for (var i = 0; i < data.affiliation.length; i++) {
                        var obj = {};
                        obj.id = data.affiliation[i];
                        obj.attrName = data.affiliation[i];
                        this.attrs.push(obj);
                    }
                    /*=====获取渠道名称======*/
                    for (var i = 0; i < data.channelName.length; i++) {
                        var obj = {};
                        obj.id = data.channelName[i];
                        obj.channelName = data.channelName[i];
                        this.channels.push(obj);
                    }
                    /*=====获取代理商数据======*/
                    for (var i = 0; i < data.agent.length; i++) {
                        var obj = {};
                        obj.id = data.agent[i];
                        obj.agentName = data.agent[i];
                        this.agents.push(obj);
                    }
                    /*=====获取平台======*/
                    for (var i = 0; i < data.platform.length; i++) {
                        var obj = {};
                        obj.id = data.platform[i];
                        obj.platFormName = data.platform[i];
                        this.platForms.push(obj);
                    }
                    /*=====获取结算方式数据======*/
                    for (var i = 0; i < data.paytype.length; i++) {
                        var obj = {};
                        obj.id = data.paytype[i];
                        obj.settlWayName = data.paytype[i];
                        this.settlWays.push(obj);
                    }
                    /*=====获取广告位置数据======*/
                    for (var i = 0; i < data.advPosition.length; i++) {
                        var obj = {};
                        obj.id = data.advPosition[i];
                        obj.adPlaceName = data.advPosition[i];
                        this.adPlaces.push(obj);
                    }
                } else {
                    this.errorFun(data['msg'])
                }
            })
        },
        /*====================时间格式化转换函数============================*/
        formatDate(date) {
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            m = m < 10 ? '0' + m : m;
            var d = date.getDate();
            d = d < 10 ? ('0' + d) : d;
            return y + '-' + m + '-' + d;
        },
        /*==================获取表格数据========================*/
        getTableData(data, btn) {
            this.activeIndex = btn;
            this.loads();
            let date1 = this.formatDate(this.ruleForm.date[0]);
            let date2 = this.formatDate(this.ruleForm.date[1]);
            this.$http.jsonp(`${this.SRC}data/get.do?p=${this.DP.p}&f=${this.DP.f}&a=search`, {
                params: { 'ps': `sday=${date1}&eday=${date2}&agent=${data.agent}&platform=${data.platForm}&affiliation=${data.attr}&channelName=${data.channel}&paytype=${data.settlWay}&advPosition=${data.adPlace}&uid=${this.DP.uid}` }
            }).then(function(res) {
                var data = JSON.parse(JSON.parse(res.body));
                this.loads();
                if (parseInt(data['res']) === 0) {
                    this.tableData = [];
                    this.tableDataAll = [];
                    if (Number(btn) == 0) {
                        this.tableHead = this.tableHead1;
                        this.tableDataAll = data.newRemainDatas;
                        this.dataTotal = this.tableDataAll.length;
                        this.tableControl();
                    } else {
                        this.tableHead = this.tableHead2;
                        this.tableDataAll = data.totalPayDatas;
                        this.dataTotal = this.tableDataAll.length;
                        this.tableControl();
                    }
                } else {
                    this.errorFun(data['msg'])
                }
            })
        },
        /*===========================查询功能===============================*/
        search(data, btn) {
            btn = this.activeIndex;
            this.loads();
            let date1 = this.formatDate(this.ruleForm.date[0]);
            let date2 = this.formatDate(this.ruleForm.date[1]);
            this.$http.jsonp(`${this.SRC}data/get.do?p=${this.DP.p}&f=${this.DP.f}&a=search`, {
                params: { 'ps': `sday=${date1}&eday=${date2}&agent=${data.agent}&platform=${data.platForm}&affiliation=${data.attr}&channelName=${data.channel}&paytype=${data.settlWay}&advPosition=${data.adPlace}&uid=${this.DP.uid}` }
            }).then(function(res) {
                var data = JSON.parse(JSON.parse(res.body));
                this.loads();
                if (parseInt(data['res']) === 0) {
                    this.tableData = [];
                    this.tableDataAll = [];
                    if (Number(btn) == 0) {
                        this.tableHead = this.tableHead1;
                        this.tableDataAll = data.newRemainDatas;
                        this.dataTotal = this.tableDataAll.length;
                        this.tableControl();
                    } else {
                        this.tableHead = this.tableHead2;
                        this.tableDataAll = data.totalPayDatas;
                        this.dataTotal = this.tableDataAll.length;
                        this.tableControl();
                    }
                } else {
                    this.errorFun(data['msg'])
                }
            })
        },
        // 表格排序
        sortChange(column) {
            if (column.prop === null) {
                return;
            } else if (column.order === 'descending') {
                this.tableDataAll.sort(compareObject(column.prop, false));
            } else { //正序
                this.tableDataAll.sort(compareObject(column.prop, true));
            }
            this.tableControl();
        },
        // 表格下载
        excel(tableIndex, sheet) {
            var that = this;
            var tableHead;
            if(this.activeIndex === 0){
                tableHead = that.tableHead1;
            }else if(this.activeIndex === 1){
                tableData = that.tableHead2;
            }
            that.xlsxTitle = sheet+"-"+this.formatDate(that.ruleForm.date[0])+"到"+this.formatDate(that.ruleForm.date[1]);
            that.downloadExl(that.tableHead, that.tableDataAll,that.xlsxTitle,sheet);
            
        },
    },
    mounted() {
        // 初始化URL
        this.SRC = this.$store.getters.src;
        this.DP.p = localStorage.getItem('ZULONGBIPROJECTID');
        this.DP.f = localStorage.getItem('ZULONGBIPAGEID');
        this.DP.uid = localStorage.getItem('ZULONGBIUSERID');
        this.tableHead = this.tableHead1;
        this.getData();
        this.ruleForm.date = [new Date(new Date().getTime() - 7 * 86400000), new Date()];
        this.getTableData(this.ruleForm, '0');
    }
}